import { getCustCarriageByCustId, freightWithdrawalApply } from "../../../api/custCarriageController";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    integralDetails:{
      availableBalance:''
    },   //运费详情
    dfDetail:{
      amount: '',
    },
    carriageId:'',    //运费Id
  },
  onLoad(){
    this.getCustCarriageByCustId()
  },
  /**
   * 根据运费Id查询运费信息
   */
  async getCustCarriageByCustId(){
    let resolve = await getCustCarriageByCustId({
      custId: wx.getStorageSync('custId')
    })
    if (resolve.success) {
      if (resolve.data==null) {
        resolve.data = {
          availableBalance: 0
        }
      }
      this.setData({
        integralDetails: resolve.data,
      })
    }
  },
  /**
   * 输入框
   */
  setInput(e){
    let { type } = e.currentTarget.dataset
    let { value } = e.detail
    console.log(e);
    this.setData({
      ['dfDetail.'+type] :value
    })
  },
  /**
   * 兑付
   */
  async extract(){
    let self = this
    let numberReg = /^[0-9]*$/

    if (!self.data.integralDetails.id) {
      wx.showToast({
        title: '没有可提现佣金',
        icon: 'none'
      })
      return
    }

    if (self.data.dfDetail.amount=='' || self.data.dfDetail.amount==null) {
      wx.showToast({
        title: '请填写运费！',
        icon: 'none',
      })
      return
    }

    if (!numberReg.test(self.data.dfDetail.amount)) {
      wx.showToast({
        title: '请填写运费！',
        icon: 'none',
      })
      return
    }

    if (self.data.dfDetail.amount > self.data.integralDetails.availableBalance) {
      wx.showToast({
        title: '您还没有这么多运费可以提现~',
        icon: 'none',
      })
      return
    }


    let resolve = await freightWithdrawalApply({
      custCarriageId: self.data.integralDetails.id,
      ...self.data.dfDetail
    })
    if (resolve.success) {
      wx.showToast({
        title: '提现成功！',
        mask: true
      })
      setTimeout(() => {
        // self.getCustCarriageByCustId()
        self.setData({
          ['dfDetail.amount']:'',
        })
        wx.navigateBack({
          delta: 1,
        })
      }, 1500);
    }
  }
})